class template specialization
<vector>
std::vector<bool>
template < class T, class Alloc = allocator<T> > class vector; // generic templatetemplate <class Alloc> class vector<bool,Alloc>; // bool specialization
Vector of bool
This is a specialized version of vector, which is used for elements of type bool and optimizes for space.
It behaves like the unspecialized version of vector, with the following changes:
- The storage is not necessarily an array of bool values, but the library implementation may optimize storage so that each value is stored in a single bit.
- Elements are not constructed using the allocator object, but their value is directly set on the proper bit in the internal storage.
- Member function flip and a new signature for member swap.
- A special member type, reference, a class that accesses individual bits in the container's internal storage with an interface that emulates a bool reference. Conversely, member type const_reference is a plain bool.
- The pointer and iterator types used by the container are not necessarily neither pointers nor conforming iterators, although they shall simulate most of their expected behavior.
These changes provide a quirky interface to this specialization and favor memory optimization over processing (which may or may not suit your needs). In any case, it is not possible to instantiate the unspecialized template of vector for bool directly. Workarounds to avoid this range from using a different type (char, unsigned char) or container (like deque) to use wrapper types or further specialize for specific allocator types.
bitset is a class that provides a similar functionality for fixed-size arrays of bits.
Template parameters
- Alloc
- Type of the allocator object used to define the storage allocation model. By default, allocator<bool> is used, which defines the simplest memory allocation model and is value-independent.
Aliased as member type vector<bool>::allocator_type.
Member types
member type | definition | notes |
value_type | The first template parameter (bool) | |
allocator_type | The second template parameter (Alloc) | defaults to: allocator<bool> |
reference | A specific member class (see reference below) | |
const_reference | bool | |
pointer | a type that simulates pointer behavior | convertible to const_pointer |
const_pointer | a type that simulates pointer to const behavior | |
iterator | a type that simulates random access iterator behavior | convertible to const_iterator |
const_iterator | a type that simulates random access iterator to const behavior | |
reverse_iterator | reverse_iterator<iterator> | |
const_reverse_iterator | reverse_iterator<const_iterator> | |
difference_type | a signed integral type | usually the same as ptrdiff_t |
size_type | an unsigned integral type | usually the same as size_t |
Member functions
The specialization has the same member functions as the unspecialized vector, except data, emplace, and emplace_back, that are not present in this specialization.
It adds the following:
- flip
- Flip bits (public member function)
- swap
- Swap containers or elements (public member function)
Non-member class specializations
- hash<vector<bool>>
- Hash for vector (class template specialization)
Data races
Simultaneous access to different elements is not guaranteed to be thread-safe (as storage bytes may be shared by multiple bits).